package com.heima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.heima.mapper.TaskMapper;
import com.heima.pojo.Task;
import com.heima.pojo.vo.TaskVo;
import com.heima.service.TaskService;
import com.heima.utils.PageResult;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * @author 赵超
 */
@Service
@RequiredArgsConstructor
public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements TaskService {
    private final TaskMapper taskMapper;

    @Override
    public PageResult<TaskVo> list(Integer pageIndex, Integer pageSize, String isRepair) {
        // 查询Task集合
        LambdaQueryWrapper<Task> lqw = new LambdaQueryWrapper<>();
        if (StringUtil.isNotEmpty(isRepair)) {
            lqw.eq(Task::getIsRepair, "false".equals(isRepair) ? 0 : 1);
        }
        List<Task> taskList = taskMapper.selectList(lqw);

        // 将Task集合转换成TaskVO集合
        List<TaskVo> taskVoList = new ArrayList<>();
        taskList.forEach(task -> {
            TaskVo taskVo = new TaskVo();
            BeanUtils.copyProperties(task, taskVo);
            taskVoList.add(taskVo);
        });

        // 分页
        PageInfo<TaskVo> page = new PageInfo<>(taskVoList);
        long totalCount = page.getTotal();
        int totalPage = (int) ((totalCount + pageSize - 1) / pageSize);
        return new PageResult<>(pageIndex, pageSize, totalPage, totalCount, taskVoList);
    }
}
